package edu.ues21.tcs.ecommerce2011.mvc.controller;

import java.io.IOException;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.Locale;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import edu.ues21.tcs.ecommerce2011.mvc.dao.ListaPrecioDAO;
import edu.ues21.tcs.ecommerce2011.mvc.model.ListaPrecio;

/**
 * 
 * @author Acunia Marco.
 */
public class EditListaPreciosController extends HttpServlet {

	ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext(
			"/BeanLocations.xml");

	ListaPrecioDAO mDao = (ListaPrecioDAO) appContext.getBean("listaprecioDAO");

	private static final long serialVersionUID = 1L;

	protected void processRequest(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

	}

	@Override
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		String idlista = request.getParameter("id");
		Integer id = Integer.parseInt(idlista);
		ListaPrecio m = mDao.getListaPrecio(id);
		request.setAttribute("lista", m);
		request.getRequestDispatcher("view/EditListaPrecio.jsp").forward(
				request, response);
	}

	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		String desdeStr = request.getParameter("desde");
		String hastaStr = request.getParameter("hasta");
		String idlista = request.getParameter("idlista");
		String nombre = request.getParameter("nombre");

		SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd",
				new Locale("es", "ES"));
		System.out.println("++++++++++++++++++++++++++++ ");
		if (desdeStr != null && hastaStr != null) {
			ListaPrecio m = new ListaPrecio();
			Integer idint = Integer.parseInt(idlista);
			m.setIdListaPrecio(idint);
			m.setNombre(nombre);
			try {
				Date fechaDesde = new java.sql.Date(sdf.parse(desdeStr)
						.getTime());
				Date fechaHasta = new java.sql.Date(sdf.parse(hastaStr)
						.getTime());

				m.setFechaDesde(fechaDesde);
				m.setFechaHasta(fechaHasta);
			} catch (Exception e) {
			}
			mDao.save(m);
		}

		response.sendRedirect("listarLP.do");

	}

	@Override
	public String getServletInfo() {
		return "Short description";
	}// </editor-fold>

}
